/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.common.utils.SessionUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.SysMenu;
import com.thinkgem.jeesite.modules.sys.service.SysMenuService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 菜单Controller
 *
 * @author kongxiangsheng
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/sysMenu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    @RequiresPermissions("sys:sysMenu:view")
    @RequestMapping(value = {"list", ""})
    public String list(Model model) {
        List<SysMenu> list = Lists.newArrayList();
        List<SysMenu> sourceList = UserUtils.getMenuList();
        SysMenu.sortList(list, sourceList, BaseConstants.ROOT_MENU_ID, true);
        model.addAttribute("list", list);
        return "modules/sys/SysMenu/list";
    }

    @RequiresPermissions("sys:sysMenu:view")
    @RequestMapping(value = "form")
    public String form(SysMenu sysMenu, Model model) {
        if (StringUtils.isNotBlank(sysMenu.getId())) {
            sysMenu = sysMenuService.getById(sysMenu.getId());
        }
        if (StringUtils.isBlank(sysMenu.getParentId())) {
            sysMenu.setParentId(BaseConstants.ROOT_MENU_ID);
        }
        // 获取排序号，最末节点排序号+30
        if (StringUtils.isBlank(sysMenu.getId())) {
            List<SysMenu> list = Lists.newArrayList();
            List<SysMenu> sourceList = UserUtils.getMenuList();
            SysMenu.sortList(list, sourceList, sysMenu.getParentId(), false);
            if (list.size() > 0) {
                sysMenu.setSort(list.get(list.size() - 1).getSort() + 30);
            }
        }
        model.addAttribute("sysMenu", sysMenu);
        return "modules/sys/SysMenu/form";
    }

    @RequiresPermissions("sys:sysMenu:edit")
    @RequestMapping(value = "save")
    public String save(SysMenu sysMenu, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, sysMenu)) {
            return form(sysMenu, model);
        }
        sysMenuService.saveMenu(sysMenu);
        addMessage(redirectAttributes, "保存菜单'" + sysMenu.getName() + "'成功");
        return "redirect:" + adminPath + "/sys/sysMenu/";
    }

    @RequiresPermissions("sys:sysMenu:edit")
    @RequestMapping(value = "delete")
    public String delete(SysMenu sysMenu, RedirectAttributes redirectAttributes) {
        sysMenuService.removeById(sysMenu);
        addMessage(redirectAttributes, "删除菜单成功");
        return "redirect:" + adminPath + "/sys/sysMenu/";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "tree")
    public String tree() {
        return "modules/sys/SysMenu/tree";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "treeselect")
    public String treeselect(String parentId, Model model) {
        model.addAttribute("parentId", parentId);
        return "modules/sys/SysMenu/treeSelect";
    }

    /**
     * 批量修改菜单排序
     */
    @RequiresPermissions("sys:sysMenu:edit")
    @RequestMapping(value = "updateSort")
    public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
        for (int i = 0; i < ids.length; i++) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setId(ids[i]);
            sysMenu.setSort(sorts[i]);
            sysMenuService.updateSort(sysMenu);
        }
        addMessage(redirectAttributes, "保存菜单排序成功!");
        return "redirect:" + adminPath + "/sys/sysMenu/";
    }


    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeValue")
    public String treeValue(@RequestParam(required = true) String id, HttpServletResponse response) {
        String officeName = "";
        SysMenu sysMenu = sysMenuService.getById(id);
        if (null != sysMenu) {
            officeName = sysMenu.getName();
        }
        return officeName;
    }

    /**
     * isShowHide是否显示隐藏菜单
     *
     * @param extId
     * @param isShowHide
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, @RequestParam(required = false) String isShowHide, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<SysMenu> list = UserUtils.getMenuList();
        for (int i = 0; i < list.size(); i++) {
            SysMenu e = list.get(i);
            if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                if (isShowHide != null && isShowHide.equals("0") && e.getIsShow().equals("0")) {
                    continue;
                }
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }
}
